﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YJKMaterialStatistic;

namespace YJKWndrStatisticsExcel.AreaTable.Forms
{
	public partial class NetAreaStatisticalForm : System.Windows.Forms.Form
	{
		public NetAreaStatisticalForm()
		{
			this.InitializeComponent();
			this.cmbGrid.Items.Add("防火分区面积表");
			this.cmbGrid.Items.Add("主要经济技术指标表");
			this.cmbGrid.Items.Add("建筑物明细表");
			this.cmbGrid.SelectedIndex = 0;
			this.txtGridName.Text = "防火分区面积表";
			this.m_gridSet.GridName = this.txtGridName.Text;
		}

		public NetAreaStatisticalForm(string strVersion)
		{
			this.InitializeComponent();
			this.m_strRevitVersion = strVersion;
			this.cmbGrid.Items.Add("防火分区面积表");
			this.cmbGrid.Items.Add("主要经济技术指标表");
			this.cmbGrid.Items.Add("建筑物明细表");
			this.cmbGrid.SelectedIndex = 0;
			this.txtGridName.Text = "防火分区面积表";
			this.m_gridSet.GridName = this.txtGridName.Text;
			this.ReadData();
		}

		private void NetAreaStatisticalForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
		}

		private void btnSet_Click(object sender, EventArgs e)
		{
			List<string> columnListInfo = this.GetColumnListInfo();
			if (new DlgColumnSetCommon(this.m_strRevitVersion, this.cmbGrid.Text, this.m_strFileFolder, columnListInfo, ref this.m_gridSet).ShowDialog() == DialogResult.OK)
			{
				this.m_gridSet.SetColWidth = true;
			}
		}

		private List<string> GetColumnListInfo()
		{
			List<string> list = new List<string>();
			if (this.cmbGrid.Text == "防火分区面积表")
			{
				list.Add("楼层");
				list.Add("防火分区编号");
				list.Add("该区功能");
				list.Add("防火分区面积(㎡)");
				list.Add("人数计算指标");
				list.Add("计算人数(人)");
				list.Add("百人疏散指标");
				list.Add("计算宽度(m)");
				list.Add("独用楼梯宽度(没人)");
				list.Add("楼梯数量");
				list.Add("辅助宽度(每个)");
				list.Add("数量");
				list.Add("小计");
			}
			else if (!(this.cmbGrid.Text == "主要经济技术指标表") && this.cmbGrid.Text == "建筑物明细表")
			{
				list.Add("建筑物编号");
				list.Add("建筑物名称");
				list.Add("层数(地上\\地下)");
				list.Add("建筑高度(M)");
				list.Add("地上建筑面积(㎡)");
				list.Add("地下建筑面积(㎡)");
				list.Add("总建筑面积(㎡)");
			}
			return list;
		}

		private void ReadData()
		{
			try
			{
				string filename = this.m_strFileFolder + "\\" + this.m_gridSet.GridName + ".hymb";
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.Load(filename);
				XmlNodeList xmlNodeList = xmlDocument.DocumentElement.SelectNodes("GridInfo");
				if (xmlNodeList.Count == 0)
				{
					this.SetFormInitData();
				}
				else
				{
					bool flag = false;
					foreach (object obj in xmlNodeList)
					{
						XmlElement xmlElement = ((XmlNode)obj) as XmlElement;
						if (xmlElement != null)
						{
							string attribute = xmlElement.GetAttribute("RevitVersion");
							if ((!("2016" == attribute) || !(this.m_strRevitVersion != "2016")) && (!("2016" != attribute) || !(this.m_strRevitVersion == "2016")))
							{
								this.m_gridSet.FontSize = this.MMToFontUnit((double)Convert.ToSingle(xmlElement.GetAttribute("FontSize")));
								this.m_gridSet.TitleFontSize = this.MMToFontUnit((double)Convert.ToSingle(xmlElement.GetAttribute("TitleFontSize")));
								this.m_gridSet.FontName = xmlElement.GetAttribute("FontName");
								this.m_gridSet.LineWidthOut = Convert.ToInt32(xmlElement.GetAttribute("LineWidthOut"));
								this.m_gridSet.LineWidthInner = Convert.ToInt32(xmlElement.GetAttribute("LineWidthInner"));
								flag = true;
								break;
							}
						}
					}
					if (!flag)
					{
						this.SetFormInitData();
					}
				}
			}
			catch
			{
				this.SetFormInitData();
			}
		}

		private void SetFormInitData()
		{
			this.m_gridSet.FontName = "仿宋";
			if (2016 < Convert.ToInt32(this.m_strRevitVersion))
			{
				this.m_gridSet.FontSize = this.MMToFontUnit(2.5);
				this.m_gridSet.TitleFontSize = this.MMToFontUnit(3.0);
			}
			else
			{
				this.m_gridSet.FontSize = this.MMToFontUnit(3.0);
				this.m_gridSet.TitleFontSize = this.MMToFontUnit(3.5);
			}
			this.m_gridSet.LineWidthOut = 7;
			this.m_gridSet.LineWidthInner = 2;
		}

		private float MMToFontUnit(double value)
		{
			return Convert.ToSingle(value * 14.0 * 72.0 / 304.8);
		}

		private void btnDrawGrid_Click(object sender, EventArgs e)
		{
			this.m_gridSet.GridName = this.txtGridName.Text;
			this.m_gridSet.LinkFile = this.chkLink.Checked;
			this.m_gridSet.NewView = this.chkNewView.Checked;
			base.DialogResult = DialogResult.OK;
		}

		private void btnExcel_Click(object sender, EventArgs e)
		{
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.FileName = this.txtGridName.Text;
			saveFileDialog.Filter = "Excel文件|*.xls|Excel文件|*.xlsx|盈建科表格文件|*.hybg";
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				this.m_strFilePath = saveFileDialog.FileName;
				this.m_eFileType = saveFileDialog.FilterIndex;
				this.m_gridSet.GridName = this.txtGridName.Text;
				this.m_gridSet.LinkFile = this.chkLink.Checked;
				this.m_gridSet.NewView = this.chkNewView.Checked;
			}
			base.DialogResult = DialogResult.Yes;
		}

		private void Cancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void cmbGridType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.txtGridName.Text = this.cmbGrid.Text;
			this.m_gridSet.GridName = this.txtGridName.Text;
			if (this.cmbGrid.Text == "主要经济技术指标表")
			{
				this.btnSet.Enabled = false;
			}
			else
			{
				this.btnSet.Enabled = true;
			}
			this.ReadData();
		}

		private void NetAreaStatisticalForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private string m_strFileFolder = Path.Combine(Product.InstallLocation, "Templates", "NetAreaTempletlFile");

		public GridSetCommonInfo m_gridSet = new GridSetCommonInfo();

		public string m_strFilePath = "";

		public int m_eFileType;

		private string m_strRevitVersion = "";
	}
}
